<?php
/**
 * Dcolors,The Community Application for Web
 *
 * Mysql class
 *
 * Not free software
 * @author     chopin xiao <x@sunsne.com>
 * @copyright  2009 The Dcolors Group
 * @version    SVN: $Id: mysql.php 6 2009-01-11 04:10:08Z chopins.xiao $
 **/


class Mysql
{
	public $link;
	public $result;
	public function __construct($host,$dbname,$user,$pass,$charset='UTF-8')
	{
		$this->connect($host,$dbname,$user,$pass,$charset);
	}
	public function connect($host,$dbname,$user,$pass,$charset)
	{
		if(is_resource($this->link))
		{
			return $this->link;
		}
		if($this->link = mysql_connect($host,$user,$pass))
		{
			$this->select_db($dbname);
			$this->setcharset($charset);
			return true;
		}
	}
	public function setcharset($charset)
	{
		return mysql_query("SET NAMES {$charset}");
	}
	public function select_db($dbname)
	{
		return mysql_select_db($dbname,$this->link);
	}
	public function query($sql,$insert=false)
	{
		$this->result = mysql_query($sql.$this->link);
		if($insert) return $this->getOne('SELECT LAST_INSERT_ID()');
	}
	public function fetch_array()
	{
		return mysql_fetch_array($this->result);
	}
	public function getOne($sql)
	{
		$this->query($sql);
		$return = $this->fetch_array();
		return $return[0];
	}
	public function getAll($sql)
	{
		$return = array();
		$this->query($sql);
		while ($row = $this->fetch_array()) {
			$return[] = $row;
		}
		return $return;
	}
}